package org.lovelandhs.robotics.y2010.input;

import edu.wpi.first.wpilibj.Joystick;
import org.lovelandhs.robotics.y2010.input.model.DefaultModel;

/**
 *
 * @author tim
 */
public class InputManager {

	private Joystick joystick;

	private InputModel model;

	public InputManager() {
		joystick = new Joystick(1);

		model = new InputModel();
		initModel();
	}

	public InputManager(Joystick joystick, InputModel model) {
		this.joystick = joystick;
		this.model = model;
	}

	public InputManager(InputModel model) {
		this.model = model;

		joystick = new Joystick(1);
	}

	protected void initModel() {
		model = new DefaultModel();
	}

	public Button getButton(int number) {
		return model.getButton(number);
	}

	public Button getButton(String name) {
		return model.getButton(name);
	}

	public Axis getAxis(String name) {
		return model.getAxis(name);
	}

	public double getX() {
		return joystick.getX();
	}

	public double getY() {
		return joystick.getY();
	}

	public double getZ() {
		return joystick.getZ();
	}

	public void processInput() {
		for (int i = 0; i < model.buttonCount(); i++) {
			Button b = model.buttonAt(i);

			boolean state = joystick.getRawButton(b.getNumber());
			b.setPressed(state);

			if (state) {
				System.out.println("Button #" + i + " set to state: " + state);
			}
		}

		for (int i = 0; i < model.axisCount(); i++) {
			Axis a =  model.axisAt(i);

			a.notifyUpdated(joystick.getRawAxis(a.getNumber()));

			System.out.println("Axis #" + a.getNumber() + " (" +
					a.getName() + "): " + a.getValue());
		}
	}

}
